
######################################################################
# Selection of external relations working parties and data preparation
######################################################################

# Programme:  cfsp-management01-monthlyData.r
# Author:     Frank Haege, Department of Politics and Administration, University of Limerick
# Contact:		frank.haege@ul.ie

# Description
#############
# This script loads the original working party data (i.e., the version providing the meeting duration by month)
# introduced by H�ge (2016), generates new policy category, time, and Lisbon Treaty variables required for the analysis,
# and drops redundant variables and all non-external relations working party observations.
# In the cases where the policy area of an External Relations working party changed over time, 
# the working party receives a time-consistent policy code that either allocates it to the External Relations 
# or to the non-External Relations area throughout the entire study period.
# Where the working party's policy code is changed to the non-External Relations policy code,
# the working party is excluded from the subsequent analyses.
#
# Reference:
# H�ge, Frank M. (2016) Political attention in the Council of the European Union: A new dataset of working party meetings, 1995-2014.
# European Union Politics 17(4): 683-703.


# Remove all objects
rm(list = ls(all = TRUE))

# Load libraries
library(data.table)

# Set working directory
setwd("C:\\Users\\Frank\\Dropbox\\European Union\\CFSP attention\\EUP submission")

# Set options
options(stringsAsFactors=F)

# Import original working party data (monthly meeting duration version)
df = read.csv("Data management\\wpar-management09-reshape01c-monthmg.csv")

# Drop redundant variables
df = df[, c(1:11, 15)]


# Create uniform policy coding and names for working parties over time
######################################################################

# Change policy coding of European Arms Policy working party
df$wppolicy[df$id=="B11"] = "Foreign Affairs"
df$id[df$id=="B11"] = "C51"

# Change policy coding of Enlargement working party
df$wppolicy[df$id=="B02"] = "Foreign Affairs"
df$id[df$id=="B02"] = "C52"
df$wppolicy[df$id=="B21"] = "Foreign Affairs"
df$id[df$id=="B21"] = "C53"

# Change policy and working party name coding of Export Credits Group
df$wppolicy[df$id=="C34"] = "Econ. & Financial Affairs"
df$id[df$id=="C34"] = "F16"

# Change policy and working party name coding of Drugs working party
df$wppolicy[df$id=="C41"] = "General Affairs"
df$wpname[df$id=="C41"] = "Horizontal Working Party on Drugs"
df$id[df$id=="C41"] = "B04"

# Change policy and working party name coding of Communications working party
df$wppolicy[df$id=="C42"] = "General Affairs"
df$wpname[df$id=="C42"] = "Working Party on Electronic Communications/Coordination Committee for Communication and Information Systems (CIS)"
df$id[df$id=="C42"] = "B07"

# Collapse merged working parties
df = data.table(df)
df = df[, .(dur=sum(dur)), by=setdiff(names(df), c("dur"))]
df = data.frame(df)
table(df$wpname)


# Generate indicator variables for external relations and its sub-fields
########################################################################

# Generate external relations indicator
df$external = 0
df$external[df$wppolicy=="Foreign Affairs"] = 1
df$external[df$wppolicy=="Development"] = 1
table(df$wppolicy, df$external, exclude=NULL)

# Generate sub-policy variable
df$area[df$id=="A04" |
            df$id=="C03" |
            df$id=="C05" |
            df$id=="C06" |
            df$id=="C07" |
            df$id=="C31" |
            df$id=="C32" |
            df$id=="C33" |
            df$id=="C34" |
            df$id=="C35"] = "Trade"
df$area[df$id=="C39" |
            df$id=="C40" |
            df$id=="C43" |
            df$id=="C44" |
            df$id=="C47" |
            df$id=="C44" |
            df$id=="C51"] = "CSDP"
df$area[df$id=="A02" |
            df$id=="C04" |
            df$id=="C08" |
            df$id=="C09" |
            df$id=="C10" |
            df$id=="C11" |
            df$id=="C12" |
            df$id=="C13" |
            df$id=="C14" |
            df$id=="C15" |
            df$id=="C45" |
            df$id=="C48" |
            df$id=="C50" |
            df$id=="C52" |
            df$id=="C53" |
            df$id=="C16"] = "Regional"
df$area[df$id=="A05" |
            df$id=="C00" |
            df$id=="C01" |
            df$id=="C02" |
            df$id=="C17" |
            df$id=="C18" |
            df$id=="C19" |
            df$id=="C20" |
            df$id=="C21" |
            df$id=="C22" |
            df$id=="C23" |
            df$id=="C24" |
            df$id=="C25" |
            df$id=="C26" |
            df$id=="C27" |
            df$id=="C28" |
            df$id=="C29" |
            df$id=="C30" |
            df$id=="C36" |
            df$id=="C37" |
            df$id=="C38" |
            df$id=="C41" |
            df$id=="C46" |
            df$id=="C42"] = "Horizontal"
df$area[df$id=="D00" |
          df$id=="D01" |
          df$id=="D02" |
          df$id=="D03" |
          df$id=="D04" |
          df$id=="D05"] = "Development"

# Check that all working parties have been coded
table(df$wpname[df$external==1 & is.na(df$area)], exclude=NULL)
table(df$wpname[df$external==1], df$area[df$external==1], exclude=NULL)


# Generate additional variables for the analysis
################################################

# Generate new policy variable combining Foreign Affairs and Development
table(df$wppolicy, exclude=NULL)
df$wppolicy.old = df$wppolicy
df$wppolicy[df$wppolicy.old=="Foreign Affairs"] = "External Relations"
df$wppolicy[df$wppolicy.old=="Development"] = "External Relations"

# Generate Lisbon Treaty indicator
df$lisbon = 0
df$lisbon[df$year>2009] = 1
df$lisbon[df$year==2009 & (df$month=="November"| df$month=="December")] = 1
table(df$datem, df$lisbon, exclude=NULL)

# Generate second Lisbon Treaty indicator (for semester based analyses)
df$lisbonsem = 0
df$lisbonsem[df$year>2009] = 1
table(df$datem, df$lisbonsem, exclude=NULL)

# Generate time variable
df = data.table(df)
df = df[, time:=seq_len(.N), by=c("id")]
df = data.frame(df)
table(df$time, exclude=NULL)

# Reorder variables
df = df[, c("lisbon", "lisbonsem", "time", "year", "yearsem", "sem", "pres", "datem", "month", "presmonth", 
            "external", "wppolicy", "area", "dur", "nowp", "id", "wpname")]


# Restrict data to external relations
df = df[df$external==1, ]
df$external = NULL

# Save data
save(df, file="Data management\\cfsp-management01-monthlyData.RData")
